;( function( $ )
{

    var request = null;

    // 预处理控制元素
    var Elems = ( function( obj )
    {
        var newObj = {};

        for( var name in obj )
        {
            newObj[ name ] = document.getElementById( obj[ name ] ) || null;
        };

        return newObj;

    })({
        realname : "realname",
        logoutIn : "ad-js-logoutIn",
        modalBg : "modalBg",
        password : "password",
        verify : "verify",
        submit : "submit",
        exitUserBtn : "exitUserBtn",
        lcode : "ad-js-lcode",
        TLOGIN : "TLOGIN"
    });

    // 预处理地址
    var Path = {
        //checkmac : document.getElementById("TLOGIN").value || null,
        login : Elems["TLOGIN"].getAttribute("data-check") || null,
        logout : Elems["TLOGIN"].getAttribute("data-exit") || null
    };

    var shownModalHandle = function()
    {
        Elems["password"].value = "";

        var localName = Handle.getSysUserName() || null;

        if( localName )
        {
            Elems["realname"].value = localName;

            Elems["password"].focus();
        }
        else
        {
            Elems["realname"].value = '';

            Elems["realname"].focus();
        }

        return false;

    }

    $( Elems["logoutIn"] ).on('showModal', function(){

        Elems["logoutIn"].style.zIndex = 1000;
        Elems["password"].value = "";

    } );

    $( Elems["logoutIn"] ).on('showModal', shownModalHandle );

    $( Elems["logoutIn"] ).on('hideModal', function()
    {
        $( this ).css("z-index","-1");
    });

    /** 登录系统*/
    Elems["submit"].onclick = function()
    {
        var userphone = Elems["realname"];
        var password = Elems["password"];
        var verify = Elems["verify"];
        var sName = userphone.value || '';
        var sPassWord = password.value || '';
        var verifyVal = verify.value || '';
        var STRING = document.getElementsByTagName("meta")[1].getAttribute("content");

        var callbackObject = {
            context : this,
            success : function( res )
            {
                if( !res ) return false;

                if( res["status"] == 1 )
                {
                    $( Elems["logoutIn"] ).css("z-index","-1");

                    top.window.TIMEOUT.bind2();

                    toastr.success( res.info );

                    return false;
                }
                //-1 .请输入用户名
                else if( res["status"] == -1 )
                {
                    userphone.value = "";

                    userphone.focus();
                }
                // -2  请输入密码
                else if( res["status"] == -2 )
                {
                    password.value = "";

                    password.focus();
                }
                // -3 验证码错误
                else if( res["status"] == -3 )
                {
                    verify.value = "";

                    verify.focus();
                }
                //-4 账号不存在
                else if( res["status"] == -4 )
                {
                    userphone.value = "";

                    userphone.focus();
                }
                // -5 所属公司已被禁用
                else if( res["status"] == -5 )
                {
                    userphone.value = "";

                    userphone.focus();
                }
                // -6 账号被禁用
                else if( res["status"] == -6 )
                {
                    userphone.value = "";

                    userphone.focus();
                }
                // -7 密码错误
                else if( res["status"] == -7 )
                {
                    password.value = "";

                    password.focus();
                }

                Elems["lcode"].onclick();

                toastr.warning( res.info );

                $( verify.parentNode )[res["iscode"]?"removeClass":"addClass"]("ad-doc-hidden");
            }
        }

        request && request.abort();
        // hex_md5( sPassWord + STRING )
        request = Handle.Request( "post", Path["login"], "username=" + sName + "&password=" + sPassWord + "&verify=" + verifyVal, callbackObject, "json" );
    };

    Elems["exitUserBtn"].onclick = function()
    {
        window.location.reload();
    }

    Elems["lcode"].onclick = function()
    {
        var verifyimg = this.src ;

        if( verifyimg.indexOf('?')>0)
        {
            $ (this ).attr("src", verifyimg + '&random=' + Math.random() );
        }
        else
        {
            $( this ).attr( "src", verifyimg.replace( /\?.*$/,'' ) + '?' + Math.random() );
        }
    }

    $( document ).unbind("keydown").bind( "keydown", function( e )
    {
        e = e || window.event;

        if( ( e.keyCode || e.which) == 13 )
        {
            Elems["submit"].onclick();
        }
    });

})( jQuery );
